package xyz.awesomeman.datasource;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.boot.jta.atomikos.AtomikosDataSourceBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import com.mysql.jdbc.jdbc2.optional.MysqlXADataSource;

import xyz.awesomeman.entity.DBConfig1;
//���ö�����Դ
@Configuration        //放入springboot容器
@MapperScan(basePackages="xyz.awesomeman.dao.test1",sqlSessionTemplateRef="test1SqlSessionTemplate")
public class DataSource1Config {
	
	//配置数据源
	//使用springboot+jta+atomikos 分布式事物管理
	@Bean(name="test1DataSource")
	@Primary    //默认数据源
	//@ConfigurationProperties(prefix="spring.datasource.test2")        //读取配置信息
	public DataSource testDataSource(DBConfig1 dbconfig1) throws SQLException {
		MysqlXADataSource mysqlXaDataSource = new MysqlXADataSource();
		mysqlXaDataSource.setUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8");
		mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);
		mysqlXaDataSource.setPassword(dbconfig1.getPassword());
		mysqlXaDataSource.setUser(dbconfig1.getUsername());
		mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);
	
		AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean();
		xaDataSource.setXaDataSource(mysqlXaDataSource);
		xaDataSource.setUniqueResourceName("test1DataSource");
		xaDataSource.setMinPoolSize(dbconfig1.getMinPoolSize());
		xaDataSource.setMaxPoolSize(dbconfig1.getMaxPoolSize());
		xaDataSource.setMaxLifetime(dbconfig1.getMaxLifetime());
		xaDataSource.setBorrowConnectionTimeout(dbconfig1.getBorrowConnectionTimeout());
		xaDataSource.setLoginTimeout(dbconfig1.getLoginTimeout());
		xaDataSource.setMaintenanceInterval(dbconfig1.getMaintenanceInterval());
		xaDataSource.setMaxIdleTime(dbconfig1.getMaxIdleTime());
		xaDataSource.setTestQuery(dbconfig1.getTestQuery());
		return xaDataSource;
		//return DataSourceBuilder.create().build();
	}
	//����sqlsessionFactory
	@Bean(name="test1SqlSessionFactory")
	@Primary
	public SqlSessionFactory testSqlSessionFactory(@Qualifier("test1DataSource")DataSource dataSource) throws Exception {
		SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
		bean.setDataSource(dataSource);
		return bean.getObject();
	}
//	@Bean(name="test1TransactionManager")
//	@Primary
//	public DataSourceTransactionManager testTransactionManager(@Qualifier("test1DataSource")DataSource dataSource) {
//		return new DataSourceTransactionManager(dataSource);
//	}
	@Bean(name = "test1SqlSessionTemplate")
	public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
		return new SqlSessionTemplate(sqlSessionFactory);
	}
}
